﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using C1.Web.Wijmo.Controls.C1Chart;
using System.Data.OleDb;
using System.Data;

namespace C1WijmoProManagement
{
    public partial class chart : System.Web.UI.Page
    {
        static string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["datasource"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            SetChart();
        }

        private void SetChart()
        {
            //数据源
            OleDbConnection connection = new OleDbConnection(connectionStr);
            connection.Open();
            OleDbDataAdapter dbAdapter=new OleDbDataAdapter("select * from ChartDataMonthly",connection);
            DataTable dt=new DataTable();
            dbAdapter.Fill(dt);
            connection.Close();

            //加入Y轴
            CompositeChartYAxis y1 = new CompositeChartYAxis();
            y1.AutoMin = false;
            y1.AutoMax = true;
            y1.Min = 0;
            y1.Text = "销售额";
            y1.Compass = ChartCompass.West;
            C1CompositeChart1.YAxes.Add(y1);

            CompositeChartYAxis y3 = new CompositeChartYAxis();
            y3.AutoMin = false;
            y3.AutoMax = true;
            y3.Min = 0;
            y3.Text = "销售百分比";

            y3.Compass = ChartCompass.East;
            C1CompositeChart1.YAxes.Add(y3);

            C1.Web.Wijmo.Controls.C1Chart.CompositeChartSeries s = null;
            C1.Web.Wijmo.Controls.C1Chart.ChartStyle style = null;

            //柱状图
            s = new C1.Web.Wijmo.Controls.C1Chart.CompositeChartSeries();
            style = new ChartStyle();
            s.Type = ChartSeriesType.Column;
            s.Label = "销售额柱状图";
            s.HintContents = new string[dt.Rows.Count - 1];
            double v = 0d;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s.Data.X.Add(dt.Rows[i][0].ToString());
                double.TryParse(dt.Rows[i][1].ToString(), out v);
                s.Data.Y.Add(v);

            }
            s.LegendEntry = true;
            s.YAxis = 0;
            C1CompositeChart1.SeriesList.Add(s);
            C1CompositeChart1.SeriesStyles.Add(style);

            //折线图
            s = new C1.Web.Wijmo.Controls.C1Chart.CompositeChartSeries();
            style = new ChartStyle();
            s.Type = ChartSeriesType.Line;
            s.Label = "销售额曲线图";
            s.HintContents = new string[dt.Rows.Count - 1];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                s.Data.X.Add(dt.Rows[i][0].ToString());
                double.TryParse(dt.Rows[i][2].ToString(), out v);
                s.Data.Y.Add(v);

            }
            s.LegendEntry = true;
            s.YAxis = 1;
            C1CompositeChart1.SeriesList.Add(s);
            C1CompositeChart1.SeriesStyles.Add(style);

            //饼状图
            s = new C1.Web.Wijmo.Controls.C1Chart.CompositeChartSeries();
            s.Type = ChartSeriesType.Pie;
            C1.Web.Wijmo.Controls.C1Chart.PieChartSeries ps;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ps = new PieChartSeries();
                ps.LegendEntry = false;
                double.TryParse(dt.Rows[i][2].ToString(), out v);
                ps.Data = v;
                ps.Label = (1+i).ToString() + "月";
                s.PieSeriesList.Add(ps);
            }
            s.Label = "销售额饼状图";
            s.Radius = 60;
            C1CompositeChart1.SeriesList.Add(s);
        }
    }
}